package com.diamond.service.impl;

import com.diamond.mapper.AdminMapper;
import com.diamond.pojo.Admin;
import com.diamond.pojo.AdminExample;
import com.diamond.service.AdminService;
import com.diamond.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    /**
     * 登录校验
     * @param name
     * @param password
     * @return
     */
    @Override
    public Admin login(String name, String password) {

        //1.查询DB中是否有该用户
        //添加where查询条件
        AdminExample adminExample = new AdminExample();
        adminExample.createCriteria().andANameEqualTo(name);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        //2.若存在查询对应密码
        if (admins.size() > 0) {
            //用户名唯一
            Admin admin = admins.get(0);
            //将传入的pass进行md5加密再对比
            String md5 = MD5Util.getMD5(password);
            if (md5 != null) {
                if (md5.equals(admin.getaPass())) {
                    return admin;
                }
            }
        }
        return null;
    }
}
